/*
 * bsp_utils.h
 *
 * Copyright (C) 2011 - Dr.NP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @version bsp_utils.h 1.0.0
 * @package libbspcore
 * @author Dr.NP <np@bsgroup.org>
 * @update 03/21/2011
 */

#ifndef BSP_UTILS_H

#define BSP_UTILS_H                             1

#include <sys/types.h>

// Definations
#define ABUFF_INITIAL                           4096
#define ABUFF_LIST_INITIAL                      16384

#define FD_TYPE_NONE                            0
#define FD_TYPE_SERVER                          1
#define FD_TYPE_CLIENT                          2
#define FD_TYPE_CONNECTOR                       3
#define FD_TYPE_TIMER                           4
#define FD_TYPE_PIPE                            5
#define FD_TYPE_EVENT                           6
#define FD_TYPE_SIGNAL                          7
#define FD_TYPE_NORMAL                          126
#define FD_TYPE_EPOLL                           127

#ifndef _POSIX_PATH_MAX
#define _POSIX_PATH_MAX                         1024
#endif

#define MAX_FUNC_NAME_LENGTH                    256

#define TRIM_TYPE_ALL                           0
#define TRIM_TYPE_LEFT                          1
#define TRIM_TYPE_RIGHT                         2

// Macros

// Structs
struct abuff_t
{
    size_t buff_size;
    size_t data_size;
    char *buff;
};

struct fd_info_t
{
    int id;
    char type;
};

// Functions
int set_nonblock(int);
int get_max_fds();
int set_rlimit();
char *get_dir();

inline void put_u_short(char *, u_int16_t);
inline u_int16_t get_u_short(char *);
inline void put_u_int(char *, u_int32_t);
inline u_int32_t get_u_int(char *);
inline void put_u_ll(char *, u_int64_t);
inline u_int64_t get_u_ll(char *);

void gen_unique_id(char *, int);
void sig_handler(int);
void set_sig();
int daemonize();

void debug_info(const char *, ...);
void debug_hex_data(const char *, int);
char *str_replace(const char *, const char *, const char *);
void str_replace2(const char *, char *, int, const char *, const char *);
char *load_file(const char *, int *);
int trim_str(char *, char);
int strtrimncmp(const char *, const char *, size_t, int);
inline void parse_ll(const char *, long long int *);

int abuff_init();
struct abuff_t * new_abuff();
void clean_abuff(struct abuff_t *);
void free_abuff(struct abuff_t *);
int append_abuff(struct abuff_t *, char *, size_t, int);
int printf_abuff(struct abuff_t *, char *, ...);
int load_file_abuff(struct abuff_t *, const char *);
void str_replace_abuff(struct abuff_t *, const char *, const char *);
int debug_abuff(struct abuff_t *);

void * ensure_list_space(void *, size_t, size_t *, size_t);
int fdset_init(size_t);
int set_fd(int, int, char);
struct fd_info_t * get_fd(int);

#endif  /* bsp_utils.h */
